//
//  ChuCunViewController.m
//  gpos_app
//
//  Created by apple on 2018/10/15.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ChuCunViewController.h"
#import "Masonry.h"
#import "GPosAPI.h"
#import "SVProgressHUD.h"
#import "WKProgressHUD.h"
#import "XMRegisterViewController.h"
#import "ForgetPassWordViewController.h"

#define NUM @"0123456789."
#define ALPHA @"0123456789"
#define ALPHANUM @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789."

@interface ChuCunViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)UIImageView * imageView;
@property (nonatomic,strong)UITextField * onemimaTixeField;
@property (nonatomic,strong)UITextField * twomimaTixeField;
@property (nonatomic,strong)WKProgressHUD * hud;
@property (nonatomic,strong)UIButton * querenButton;

@end

@implementation ChuCunViewController

//改变状态栏
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    self.tabBarController.tabBar.hidden=YES;
//    self.navigationController.navigationBar.topItem.title = @"储存";
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];;
    self.navigationController.navigationBar.hidden = NO;
    //去掉返回的文字
    self.navigationController.navigationBar.topItem.title = @"";
    self.navigationController.navigationBar.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBar.tintColor=[UIColor blackColor];
    self.title = @"储存";
    [self UIloadView];
}

- (void)UIloadView{
    
    self.imageView = [[UIImageView alloc] init];
    self.imageView.image = [UIImage imageNamed:@"icon_chucun"];
    [self.view addSubview:self.imageView];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(kNavbarHeight+50);
        make.centerX.equalTo(self.view);
    }];
    
    UILabel * jxjeLabel = [[UILabel alloc] init];
    jxjeLabel.text = @"储存金额";
    jxjeLabel.textColor = [UIColor blackColor];
    jxjeLabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:jxjeLabel];
    
    [jxjeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageView.mas_bottom).with.offset(70);
        make.left.equalTo(self.view).with.offset(25);
        //        make.width.equalTo(@60);
    }];
    
    UILabel * danweiLabel = [[UILabel alloc] init];
    danweiLabel.text = @"GPOS";
    danweiLabel.textColor = [UIColor blackColor];
    danweiLabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:danweiLabel];
    
    [danweiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(jxjeLabel);
        make.right.equalTo(self.view).with.offset(-25);
        //        make.width.equalTo(@60);
    }];
    
    self.onemimaTixeField = [[UITextField alloc] init];
    self.onemimaTixeField.placeholder =@"请输入储存的金额";
    self.onemimaTixeField.keyboardType=UIKeyboardTypeDecimalPad;
    self.onemimaTixeField.font = [UIFont systemFontOfSize:14.f];
    self.onemimaTixeField.delegate = self;
    //    self.onemimaTixeField.secureTextEntry = YES;
    self.onemimaTixeField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:self.onemimaTixeField];
    [self.onemimaTixeField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(jxjeLabel);
        make.left.equalTo(jxjeLabel.mas_right).with.offset(20);
        make.width.equalTo(@160);
    }];
    
    UIView * oneView = [[UIView alloc] init];
    oneView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:oneView];
    [oneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(jxjeLabel.mas_bottom).with.offset(5);
        make.left.equalTo(self.view).with.offset(25);
        make.right.equalTo(self.view).with.offset(-25);
        make.height.equalTo(@1);
    }];
    
    UILabel * jymimaLabel = [[UILabel alloc] init];
    jymimaLabel.text = @"交易密码";
    jymimaLabel.textColor = [UIColor blackColor];
    jymimaLabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:jymimaLabel];
    
    [jymimaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(oneView.mas_bottom).with.offset(25);
        make.left.equalTo(self.view).with.offset(25);
        //        make.width.equalTo(@60);
    }];
    
    UIButton * wjmimaButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [wjmimaButton setTitle:@"忘记密码 ?" forState:UIControlStateNormal];
    wjmimaButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [wjmimaButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.view addSubview:wjmimaButton];
    [wjmimaButton addTarget:self action:@selector(wjmimaButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [wjmimaButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(jymimaLabel);
        make.right.equalTo(self.view).with.offset(-25);
        make.width.equalTo(@70);
    }];
    
    self.twomimaTixeField = [[UITextField alloc] init];
    self.twomimaTixeField.placeholder =@"请输入交易密码";
    self.twomimaTixeField.keyboardType=UIKeyboardTypeNumberPad;
    self.twomimaTixeField.font = [UIFont systemFontOfSize:14.f];
    self.twomimaTixeField.delegate = self;
    self.twomimaTixeField.secureTextEntry = YES;
    self.twomimaTixeField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:self.twomimaTixeField];
    [self.twomimaTixeField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(jymimaLabel);
        make.left.equalTo(jymimaLabel.mas_right).with.offset(20);
        make.width.equalTo(@130);
    }];
    
    UIView * twoView = [[UIView alloc] init];
    twoView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:twoView];
    [twoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(jymimaLabel.mas_bottom).with.offset(5);
        make.left.equalTo(self.view).with.offset(25);
        make.right.equalTo(self.view).with.offset(-25);
        make.height.equalTo(@1);
    }];
    
   self.querenButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.querenButton setBackgroundImage:[UIImage imageNamed:@"icon_anniu_f"] forState:UIControlStateNormal];
    [self.querenButton setTitle:@"确认" forState:UIControlStateNormal];
    [self.querenButton addTarget:self action:@selector(querenButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.querenButton];
    
    
    [self.querenButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.left.equalTo(self.view).with.offset(20);
        make.right.equalTo(self.view).with.offset(-20);
        make.top.equalTo(twoView.mas_bottom).with.offset(30);
    }];
 
    
}

//忘记密码wjmimaButtonAction
- (void)wjmimaButtonAction:(UIButton *)sender {
    
    ForgetPassWordViewController * forgitPassWordVC = [[ForgetPassWordViewController alloc] init];
    forgitPassWordVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:forgitPassWordVC animated:YES];
    forgitPassWordVC.hidesBottomBarWhenPushed = NO;
    
}

//储存按钮
- (void)querenButtonAction:(UIButton *)sender {
    
    if (self.onemimaTixeField.text.length==0) {
        self.hud=[WKProgressHUD popMessage:@"请输入储存金额" inView:self.view duration:1 animated:YES];
        return;
    }
    if (self.twomimaTixeField.text.length==0) {
        self.hud=[WKProgressHUD popMessage:@"请输入交易密码" inView:self.view duration:1 animated:YES];
        return;
    }
    if (self.twomimaTixeField.text.length!=6) {
        self.hud=[WKProgressHUD popMessage:@"密码不正确" inView:self.view duration:1 animated:YES];
        return;
    }
    
    __weak typeof(self) weakSelf = self;
    int user = [[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] intValue];
    NSDictionary * params = @{@"amount" : self.onemimaTixeField.text,
                              @"passWord" : self.twomimaTixeField.text,
                              @"userId" : @(user),
                              
                              };
    NSLog(@"oj---%@",params);
    self.querenButton.userInteractionEnabled=NO;
    [GPosAPI requestStorageAmountWithParams:params andCallback:^(id obj) {
        NSLog(@"储存---%@",obj);
        if (obj==nil) {
            weakSelf.hud=[WKProgressHUD popMessage:@"请求失败" inView:self.view duration:1 animated:YES];
            weakSelf.querenButton.userInteractionEnabled=YES;
        }else{
            
            if ([[obj objectForKey:@"code"] intValue] == 200) {
//                weakSelf.hud=[WKProgressHUD popMessage:[obj objectForKey:@"message"] inView:self.view duration:1 animated:YES];
                [SVProgressHUD showSuccessWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                NSNotification *LoseChuCun = [NSNotification notificationWithName:@"buttonLoseRespon" object:nil];
                
                //                    2.发送通知:
                [[NSNotificationCenter defaultCenter] postNotification:LoseChuCun];
                
                [weakSelf.navigationController popViewControllerAnimated:YES];
                weakSelf.querenButton.userInteractionEnabled=YES;
                
            }else if ([[obj objectForKey:@"code"] intValue] == 401 || [[obj objectForKey:@"code"] intValue] == 402){
                UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
                //storyboard里的ID
                XMRegisterViewController *vc = [mainStory instantiateInitialViewController];
                vc.navigationController.navigationBarHidden=NO;
                //                    //设置当前窗口的根控制器
                [UIApplication sharedApplication].keyWindow.rootViewController=vc;
                weakSelf.querenButton.userInteractionEnabled=YES;
                
                [SVProgressHUD showInfoWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                
            }else{
                
                weakSelf.hud=[WKProgressHUD popMessage:[obj objectForKey:@"message"] inView:self.view duration:1 animated:YES];
                weakSelf.querenButton.userInteractionEnabled=YES;
            }
            
            
        }
        
        
    }];
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.twomimaTixeField ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.twomimaTixeField.text.length >= 6) {
            self.twomimaTixeField.text = [textField.text substringToIndex:6];
            
            return NO;
        }
        //设置密文切换时保持之前的输入内容
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHA] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        return [string isEqualToString:filtered];
    }
    
    if (textField == self.onemimaTixeField ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.onemimaTixeField.text.length >= 10) {
            self.onemimaTixeField.text = [textField.text substringToIndex:10];
            
            return NO;
        }
        //设置密文切换时保持之前的输入内容
        if ([string isEqualToString:@"."] && ([textField.text rangeOfString:@"."].location != NSNotFound || [textField.text isEqualToString:@""])) {
            return NO;
        }
        
        NSMutableString *str = [[NSMutableString alloc] initWithString:textField.text];
        [str insertString:string atIndex:range.location];
        if (str.length >= [str rangeOfString:@"."].location+6){
            return NO;
        }
        
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUM] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        return [string isEqualToString:filtered];
    }
    
    return YES;
}
@end
